import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "@/layout/index.vue";

export const routes = [
  {
    path: "/",
    name: "Layout",
    redirect: "/login",
    meta: {
      background: "gray",
      // title: "布局路由",
    },
    component: Layout,
    children: [
      {
        path: "/index",
        name: "Index",
        redirect: "/home",
        component: () => import("../views/home/index.vue"),
        meta: {
          background: "indianred",
          title: "首页（一级菜单）",
          icon: "icon-huanjingshuju"
        },
        children: [
          {
            path: "/home",
            name: "Home",
            component: () => import("../views/BgTest1.vue"),
            meta: {
              title: "(临时首页)",
              icon: "icon-fenjiandengji"
            },
          },
        ],
      },
      {
        path: "/noticeboard",
        name: "NoticeBoard",
        component: () => import("../views/NoticeBoard.vue"),
        meta: {
          background: "#345",
          title: "公告页",
          hidden: true
        },
      },
      {
        path: "/bgtest1",
        name: "BgTest1",
        component: () => import("../views/BgTest1.vue"),
        meta: {
          background: "burlywood",
          icon: "icon-biaozhungongxuguanli",
          title: "body背景色测试一",
        },
      },
      {
        path: "/bgtest2",
        name: "BgTest2",
        component: () => import("../views/BgTest2.vue"),
        meta: {
          background: "cadetblue",
          icon: "icon-cangkuguanli",
          title: "body背景色测试二",
        },
      },
      {
        path: "/:pathMatch(.*)*",
        name: "404",
        meta: {
          background: "cornsilk",
          title: "404页面",
          hidden: true,
        },
        component: () => import("../views/404.vue"),
      },
      // {
      //   path:'/login',
      //   name: 'login',
      //   component: () => import('@/views/login.vue'),
      //   meta: {
      //     hidden: true
      //   }
      // }
    ],
  },
  {
    path: "/login",
    name: "Login",
    meta: {
      hidden: true,
      title: "登录页"
    },
    component: () => import("@/views/login.vue"),
  },
];

const router = createRouter({ 
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes,
});

router.beforeEach((to) => {
  // console.log(to,'to')
  // 设置背景色
  document
    .querySelector("body")
    .setAttribute("style", "background-color:" + to.meta.background);
});

// router.beforeEach((to, from, next) => {
//   if (!getStoreJson("CurUser") && to.path !== "/") {
//     // 如果没有登录 先登录
//     next({
//       path: "/",
//     });
//   } else {
//     if (to.path === "/" || to.path == "/404" || to.path == "/500") {
//       // 不加会死循环
//       next();
//     } else {
//       //判断访问的页面是否授权
//       var isAuth = isAuthorized(to.path);
//       if (!isAuth) {
//         //如果没有授权,自动跳转到404页面
//         next({
//           path: "/404",
//         });
//       } else {
//         //如果授权,继续下一步
//         next();
//       }
//     }
//   }
// });

export default router;
